<template>
  <q-page class="q-pa-md">
    <div class="row items-center">
      <div class="q-pb-md text-h5">
        {{ pageTitle }}
        <span class="text-subtitle1">{{$t('列表')}}</span>
      </div>
      <q-space></q-space>
      <search-bar-toggle-btn v-model="searchBarVisibility"></search-bar-toggle-btn>
    </div>
    <div class="q-pa-sm shadow-2 q-mb-md items-center" v-show="searchBarVisibility">
      <div class="row q-col-gutter-sm">
        <div class="col-xs-8 col-sm-6 col-md-5 col-lg-3 row q-col-gutter-sm">
          <div class="col-3 text-right searchbar-label">{{$t('代码名称')}}</div>
          <div class="col-9">
            <q-input @focus="ui.select" @keyup.enter="loadListData(2)" :maxlength="30" square
              :hide-bottom-space="true" v-model="fpsReq.codeName" dense outlined stack-label
              :readonly="listDataLoadingState" clearable />
          </div>
        </div>
        <div>
          <q-btn color="primary" icon="search" :label="$t('查询')" no-caps
            @click="loadListData(2)" :disable="listDataLoadingState" />
        </div>
      </div>
    </div>

    <div class="shadow-2">
      <q-table @request="fpsAssist.onDataTableRequest" :rows="$d.tableData" :columns="fpsInfo.columnsInfo"
        separator="cell" row-key="userName" v-model:pagination="fpsInfo.paginationInfo"
        :visible-columns="fpsInfo.visibleColumns" dense flat :hide-pagination="true" :loading="listDataLoadingState"
        color="primary" binary-state-sort @row-dblclick="(evt,row)=>{fpsNav.gotoDetail(row.codeName)}">
        <!--:visible-columns 控制切换-->
        <template v-slot:top="props">
          <!--填充所有剩余空间-->
          <q-space />
          <table-col-options-cfg v-model:table-columns="fpsInfo.columnsInfo"
            v-model:visible-columns="fpsInfo.visibleColumns"
            @update-col-options-for-save="fpsAssist.saveColOptions">
          </table-col-options-cfg>
          <q-btn flat dense :icon="props.inFullscreen ? 'fullscreen_exit' : 'fullscreen'"
            :title="props.inFullscreen ? $t('退出全屏') : $t('全屏显示')" @click="props.toggleFullscreen" class="q-ml-sm"
            :ripple="false" />
        </template>
        <template v-slot:no-data>
          <div class="full-width items-center justify-center row">
            <q-icon name="error_outline" />
            {{$t('无数据')}}
            <q-btn flat icon="refresh" :loading="listDataLoadingState"
              :disable="listDataLoadingState" dense :title="$t('刷新')" size="sm" class="q-ml-sm" @click="loadListData"></q-btn>
          </div>
        </template>
        <!--操作列插槽-->
        <template v-slot:body-cell-operation="props">
          <q-td :props="props" auto-width>
            <q-btn color="primary" :label="$t('查看')" no-caps class="btn-in-row" :dense="true"
              @click="evt => fpsNav.gotoDetail(props.row.codeName, evt)" />
          </q-td>
        </template>
        <template v-slot:bottom>
          <paging-bar :pageNo="fpsReq.pageNo" :pageSize="fpsReq.pageSize" :totalAmount="fpsInfo.totalAmount"
            @pagesize-changed="fpsAssist.onPageSizeChanged" @go-to-page="fpsNav.gotoPage"
            :loading-state="listDataLoadingState" />
        </template>
        <template v-slot:body-cell-immutable="props">
          <q-td :props="props" auto-width>
            <q-badge v-if="props.row.immutable" color="primary" :label="$t('是')" />
          </q-td>
        </template>
      </q-table>
    </div>
  </q-page>
</template>

<script lang="ts">
export default {
  name: 'codeListStd'
}
</script>

<script lang="ts" setup>
import {useTitleHelper} from '../../hooks/titleHelper'
import {reactive, ref} from 'vue'
import {Code, GetCodeListReq, GetCodeListResp} from '../../defs/code'
import {ColumnInfo} from '../../defs/comm'
import {useFpsHelper} from '../../hooks/fpsHelper'
import {useI18n} from 'vue-i18n'

const {t} = useI18n({useScope: 'global'})

const pageTitle = t('代码设置')
useTitleHelper(pageTitle)

// 是否处于数据列表加载的状态
let listDataLoadingState = ref(false)

// 查询参数
let fpsReq: GetCodeListReq = reactive({
  codeName: null,
  pageNo: 1,
  pageSize: 40,
  isDescending: false,
  orderBy: null
})

// 列参数
const columnsDefine: ColumnInfo[] = [
  {
    label: t('操作'),
    name: 'operation',
    field: 'operation',
    align: 'left',
    defOrder: 1,
    defVisible: true
  },
  {
    label: t('代码ID'),
    name: 'codeId',
    field: 'codeId',
    align: 'left',
    sortable: true,
    defOrder: 2,
    defVisible: true
  },
  {
    label: t('代码名称'),
    name: 'codeName',
    field: 'codeName',
    align: 'left',
    defOrder: 3,
    defVisible: true
  },
  {
    label: t('不可修改'),
    name: 'immutable',
    field: 'immutable',
    align: 'left',
    defOrder: 4,
    defVisible: true
  },
  {
    label: t('描述'),
    name: 'descriptions',
    field: 'descriptions',
    align: 'left',
    defOrder: 5,
    defVisible: true
  }
]

const $d = reactive({
  tableData: [] as Code[]
})

const loadListData = function (resetFpsReq?: number) {
  if (resetFpsReq) {
    if (resetFpsReq >= 1) {
      fpsReq.pageNo = 1
    }
    if (resetFpsReq >= 2) {
      fpsReq.orderBy = ''
      fpsReq.isDescending = false
    }
  }
  listDataLoadingState.value = true
  $f.axios.get<GetCodeListResp>('/comm/code/get-code-list', {params: fpsReq})
    .then((result) => {
      fpsAssist.handleGenericFpsResp(result.data)
      $d.tableData = result.data.codeList
      fpsAssist.saveFpsReq(fpsReq)
    })
    .catch((err) => {
      $f.ui.showNotifyFailed($f.getAxiosErrMsg(err))
    })
    .finally(() => {
      listDataLoadingState.value = false
    })
}

const {
  fpsInfo,
  fpsAssist,
  fpsNav,
  searchBarVisibility
} = useFpsHelper('/comm/cfg/code',
  '/comm/cfg/code/detail',
  null,
  null,
  fpsReq, columnsDefine,
  null,
  loadListData)

// region 初始化动作
const ui = $f.ui
// endregion

</script>
